import { Injectable } from '@angular/core';
import * as moment from 'moment';
import { UpdateModelService } from '../model/update-model.service';
import { UInfoModel } from './uuid.model';
import { ModelListService } from '../project/model/model-list.service';
declare var Neon: any;

interface bindTaskConfig {
  taskName: string;
  uuids: string[];
}

@Injectable()
export class GanttApi {
  public tapi: any;
  public constructor(private updateModelService: UpdateModelService, private baseService: ModelListService, ) {
    if (gum.Apis.getApi(this.baseService.leftBench)) {
      this.tapi = gum.Apis.getApi(this.baseService.leftBench)['3D'];
    }

  }
  // 初始化
  public init(param: Map<number, { uuid: string; info: UInfoModel }>) {
    return Neon.taskApis.init(param);
  }

  // 渲染任务绑定的构件
  public renderTask(symbol: number | number[]) {
    return Neon.taskApis.renderUuid(symbol);
  }

  // 渲染任务绑定的所有构件
  public renderUuidsFromTask(symbol: number | number[]) {
    return Neon.taskApis.renderUuidsFromTask(symbol);
  }

  // 取消选择


  // 销毁
  public dispose() {
    return Neon.taskApis.dispose();
  }

  // 绑定任务
  public bindTask(param: bindTaskConfig) {
    return Neon.taskApis.bindTask(param);
  }

  // 清除绑定任务
  public clearTask() {
    return Neon.taskApis.clearTask();
  }

  // 透明显示
  public selectOpicaty() {
    return Neon.taskApis.selectOpicaty();
  }

  // 清除高亮构件，在取消选择时调用
  public unSelect() {
    return Neon.taskApis.unSelect();
  }

  public hiddenUuid(uuid: string) {
    return Neon.taskApis.hiddenUuid(uuid);
  }

  public hiddenTask(symbol: number | number[]) {
    return Neon.taskApis.hiddenTask(symbol);
  }

  public setCallBack(name: string, callBack: { fn: Function; target: any; arguments: any }) {
    return Neon.taskApis.setCallBack(name, callBack);
  }

  // 追加构件
  public addFragment() {
    return Neon.taskApis.addFragment();
  }

  // 确认追加
  public addOver() {
    return Neon.taskApis.addOver();
  }

  // 解绑构件
  public clearSelectedUUIDS() {
    return Neon.taskApis.clearSelectedUUIDS();
  }

  // 获取选中构件的uuid
  public getSelectedUUIDs() {
    let tapi = gum.Apis.getApi(this.baseService.leftBench)['3D'];
    return tapi.getSelected();
    // return Neon.taskApis.getSelectedUUIDs();
  }

  // 更新高亮
  public setHighLight(symbol: number) {
    return Neon.taskApis.setHighLight(symbol);
  }

  // 获取任务中所有构件的信息
  public getTaskPros(symbol: number) {
    return Neon.taskApis.getTaskPros(symbol);
  }

  public initAttribute() {
    return Neon.taskApis.initAttribute();
  }

  public resetState() {
    return Neon.taskApis.resetState();
  }

  /**
   * 渲染构件uuid
   * @param uuids 
   */
  public renderUuid(uuids: string[]) {
    console.log(uuids);
    let tapi = gum.Apis.getApi(this.baseService.leftBench);
    tapi["3D"].hiddenAll();
    uuids.forEach((uid) => {
      tapi["3D"].highlightComponent(gum.atob(uid));
    });
    tapi["3D"].update();
  }

  /**
   * 返回模型初始状态
   */
  public home3D() {
    let tapi = gum.Apis.getApi(this.baseService.leftBench);
    tapi["3D"].home3D();
  }

  public renderFinishedMode(symbol: number) {
    this.resetState();
    return Neon.taskApis.renderFinishedMode(symbol);
  }


  public getMinStartDate(tasks) {
    let maxEndDate = tasks[0].endDate;
    let minStartDate = tasks[0].startDate;
    for (let i of tasks) {
      if (moment(tasks.endDate).isAfter(maxEndDate)) {
        maxEndDate = tasks[i].endDate;
      }
      if (moment(tasks.startDate).isBefore(minStartDate)) {
        minStartDate = tasks.startDate;
      }
    }

    return {
      maxEndDate,
      minStartDate
    };
  }

}
